package com.common.exception;


import com.common.result.ResultCode;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author zyl
 * @Desc
 * @date 2023/7/20 10:03
 */
public class MyException extends RuntimeException {

    private static final long serialVersionUID = 604122701395795861L;

    private ResultCode resultCode;

    public MyException() {
        super();
    }

    public MyException(String message) {
        super(String.format("====[errorMessage ：  %s]", message));
    }

    public MyException(ResultCode resultCode, String message) {
        super(message);
        this.resultCode = resultCode;
    }

    public MyException(ResultCode code) {
        super(code.getMsg());
        this.resultCode = code;
    }

    /**
     * 异常详情打印到字符串
     *
     * @param e
     * @return
     */
    public static String printToStr(Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

    /**
     * 异常详情打印到字符串
     *
     * @param e
     * @return
     */
    public static String printToStr(Throwable e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

    public ResultCode getResultCode() {
        return resultCode;
    }

    public void setResultCode(ResultCode resultCode) {
        this.resultCode = resultCode;
    }
}
